POJ - 2240 Arbitrage (最短路 Floyd算法 && Bellman-Ford算法)

Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent. 

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not. 
Input
The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. 
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
Sample Input
3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0
Sample Output
Case 1: Yes

Case 2: No

题意:

钱币套汇问题,就是利用货币之间的汇率差异,从而将一单位的某种货币,兑换回多于一单位的同种货币。题目每组数据给出N种货币,M种汇率关系,每种关系是每单位的前者兑换后者的汇率(单向)。问通过这些汇率兑换会不会出现套汇现象。

解题思路:

其实每种货币可以看成一个点,货币之间的汇率看成这两点构成的边的权值,构造好图之后就是求从一点出发,看经过几条路径之后可以得到比自身大的值(但是注意路径上的权值是相乘的关系不是相加)。

Floyd算法:

 因为最多30种货币,所以用Flayd算法也可以,Floyd算法的思想也比较好理解,主要就是图的构造,(看代码注释)需要注意的是,题目要求最大汇率关系,所以更新数据时是求大于本身的,并且权值为相乘关系,具体看代码注释。

AC代码:

#include<stdio.h>
#include<string.h>

char huo[36][36];
char aa[36], bb[36];
double mp[36][36], x;

int main()
{
    int n, g = 1;
    while( scanf("%d",&n) && n )
    {
        for( int i = 1; i <= n; i++ )
            scanf("%s",huo[i]);     //存入货币种类
        int m;                      //初始化图,因为没有汇率关系的
        memset(mp,0,sizeof(mp));    //货币之间不能兑换,赋值为0
        for( int i = 1; i <= n; i++ )
            mp[i][i] = 1;           //同种货币之间汇率为1
        scanf("%d",&m);
        for( int i = 1; i <= m; i++ )
        {
            int x1, x2;
            scanf("%s%lf%s",aa,&x,bb);
            for( int j = 1; j <= n; j++ )
            {
                if( strcmp(huo[j],aa) == 0 ) //查找是第几种货币,转化为点
                {
                    x1 = j;
                    break;
                }
            }
            for( int j = 1; j <= n; j++ )
            {
                if( strcmp(huo[j],bb) == 0 ) //查找是第几种货币,转化为点
                {
                    x2 = j;
                    break;
                }
            }
            mp[x1][x2] = x;   //赋权值
        }
        for( int k = 1; k <= n; k++ )
        {
            for( int i = 1; i <= n; i++ )
            {
                for( int j = 1; j <= n; j++ )
                {
                    if( mp[i][j] < mp[i][k] * mp[k][j] ) //因为找最大汇率关系,所以为“<”和“*”
                        mp[i][j] = mp[i][k] * mp[k][j];
                }
            }
        }
        int flag = 0;
        for( int i = 1; i <= n; i++ )
        {
            if( mp[i][i] > 1 ) //发现有超过本身的值,说明有套汇现象
            {
                flag = 1;
                break;
            }
        }
        printf("Case %d: ",g++);
        if( flag )
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

Bellman-Ford算法:

注意之前该算法从源点V0到顶点V的最短路经最多有n-1条边;但在本题中,由于要找到一条回路,回到源点V0,所以最多有n条边,且多于n条边是没有必要的,因为如果存在套汇的话,n条边构成的回路就能形成套汇,具体看代码注释。

AC代码:

#include<stdio.h>
#include<string.h>
#define maxn 50 //顶点数最大值
#define maxm 1000 //边数最大值
#define max(a,b) ( (a)>(b) ? (a):(b) )

struct exchange //汇率关系
{
    int ci, cj;
    double cij;
}ex[maxm]; //汇率关系数组

int i, j, k; //循环变量
int n, m;    //货币种类、汇率数目
int flag, kase = 0;
char name[maxn][20], a[20], b[20];
double x, maxdist[maxn]; //汇率、源点i到其他每个顶点(包括它本身)的最长路径长度

int readcase() //读入数据
{
    scanf("%d",&n);
    if( n == 0 )
        return 0;
    for( i = 0; i < n; i++ ) //读入n个货币名称
        scanf("%s",name[i]);
    scanf("%d",&m);
    for( int i = 0; i < m; i++ ) //读入汇率
    {
        scanf("%s%lf%s",a,&x,b);
        for( j = 0; strcmp(a,name[j]); j++ )
            ;
        for( k = 0; strcmp(b,name[k]); k++ )
            ;
        ex[i].ci = j; ex[i].cij = x; ex[i].cj = k;
    }
    return 1;
}

//Bellman-Ford算法;以顶点v0为源点,求它到每个顶点(包括它本身)的最大距离
void bellman( int v0 )
{
    flag = 0;
    memset(maxdist,0,sizeof(maxdist));
    maxdist[v0] = 1;
    for( k = 0; k < n; k++ ) //从maxdist(0)递推到maxdist(1),...,maxdist(n)
    {
        for( i = 0; i < m; i++ ) //判断每条边,加入它是否能使得最大距离增加
        {
            if( maxdist[ex[i].ci] * ex[i].cij > maxdist[ex[i].cj] )
                maxdist[ex[i].cj] = maxdist[ex[i].ci] * ex[i].cij;
        }
    }
    if( maxdist[v0] > 1.0 )
        flag = 1;
}

int main()
{
    while( readcase() ) //读入货币种类
    {
        for( i = 0; i < n; i++ )
        {
            bellman(i);  //从第i个顶点出发求最长路径
            if( flag ) break;
        }
        if(flag) printf("Case %d: Yes\n",++kase);
        else printf("Case %d: No\n",++kase);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值